package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.hdw.CabinetBox;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.biz.client.ClientBizUtils;
import cc.yugu.yhdw.webserver.service.hdw.CabinetBoxService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/security/hdw/cabinet_box")
public class CabinetBoxController extends SecurityController {

    @Autowired
    CabinetBoxService cabinetBoxService;

    @RequestMapping(value ="index.htm")
    public void index(String cabinetId, int editFlag, Model model) {
        model.addAttribute("cabinetId", cabinetId);
        model.addAttribute("editFlag", editFlag);
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(CabinetBox terminalBox) {
        return PageResult.successResult(cabinetBoxService.findPage(terminalBox));
    }

    @RequestMapping("add.htm")
    @ViewModel(ViewModel.INNER_PAGE)
    public void add(String cabinetId, Model model) {
        model.addAttribute("cabinetId", cabinetId);
    }

    @RequestMapping("batch_add.htm")
    @ViewModel(ViewModel.INNER_PAGE)
    public void batch_add(String cabinetId, Model model) {
        model.addAttribute("cabinetId", cabinetId);
    }

    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(CabinetBox terminalBox) {
        return cabinetBoxService.insert(terminalBox);
    }

    @RequestMapping("batch_create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult batchCreate(String cabinetId, String batchBox) {
        cabinetBoxService.batchInsert(cabinetId, batchBox);
        return ExtResult.successResult();
    }

    @RequestMapping("edit.htm")
    @ViewModel(ViewModel.INNER_PAGE)
    public String edit(String cabinetId, String boxNum, Model model) {
        CabinetBox terminalBox = cabinetBoxService.find(cabinetId, boxNum);
        if(terminalBox == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        if(terminalBox.getBoxStatus() != CabinetBox.BoxStatus.EMPTY.getValue()) {
            model.addAttribute("message", "箱子已经使用，不能修改");
            return SEGMENT_MESSAGE;
        }

        model.addAttribute("entity", terminalBox);
        return "/security/hdw/cabinet_box/edit";
    }

    @RequestMapping("update")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(CabinetBox entity) {
        return cabinetBoxService.update(entity);
    }

    @RequestMapping("clean")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult clean(CabinetBox entity) {
        return cabinetBoxService.clean(entity);
    }

    @RequestMapping("view.htm")
    @ViewModel(ViewModel.INNER_PAGE)
    public String view(String cabinetId, String boxNum, Model model) {
        CabinetBox terminalBox = cabinetBoxService.find(cabinetId, boxNum);
        if(terminalBox == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        model.addAttribute("entity", terminalBox);
        return "/security/hdw/cabinet_box/view";
    }

    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(String cabinetId, String boxNum) {
        return cabinetBoxService.delete(cabinetId, boxNum);
    }

    @RequestMapping("open_box.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult openBox(String cabinetId, String boxNum) throws InterruptedException {
        return ClientBizUtils.noticeOpenBox(appConfig, cabinetId, boxNum);
    }
}
